package testAndSomeTries.motionDetection;

import java.awt.image.*;

/**
 * 
 * Class of an image represented by some Pixel
 * @author HINB
 *
 * @see Pixel
 */
public class ImagePix
{
	
	private static final long serialVersionUID = 1;

	/* *********************************************************
	 * Attributes
	 * ******************************************************** */	
	/**
	 * Width of the image
	 */
	private int _width;
	
	/**
	 * Height of the image
	 */
	private int _height;
	
	/**
	 * Set of Pixel
	 */
	private Pixel[][] _pix;
	            
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */
	/**
	 * Create an ImagePix with only one black Pixel
	 */
	public ImagePix()
	{
		_width = 1;
		_height = 1;
		_pix = new Pixel[1][1];
		_pix[0][0] = new Pixel();

	}
	
	
	/**
	 * Create an ImagePix with the specified size and only black Pixel
	 * @param width width of the image
	 * @param height height of the image
	 */
	public ImagePix(int width, int height)
	{
		_width = width;
		_height = height;
		_pix = new Pixel[width][height];
		
		for(int j=0; j<height; j++)
		{
			for(int i=0; i<width; i++)
			{
				_pix[i][j] = new Pixel(0, 0, 0);			
			}
		}
	}
	
	
	/**
	 * Create an ImagePix from an initial ImagePix
	 * @param image original ImagePix
	 */
	public ImagePix(ImagePix image)
	{
		_width = image.getWidth();
		_height = image.getHeight();
		_pix = new Pixel[_width][_height];
		
		int red;
		int green;
		int blue;
		
		for(int j=0; j<_height; j++)
		{
			for(int i=0; i<_width; i++)
			{
				red = image.getPixel(i, j).getRed();
				green = image.getPixel(i, j).getGreen();
				blue = image.getPixel(i, j).getBlue();
				_pix[i][j] = new Pixel(red, green, blue);			
			}
		}
	}
	
	
	/* *********************************************************
	 *  Transform an imageRGB in imageGray 
	 * ********************************************************* */
	/**
	 * Create an ImagePix from an initial ImagePix and transform it in a gray image
	 * @param image original ImagePix
	 * @param gray indication of the transformation
	 */
	public ImagePix(ImagePix image, String gray)
	{
		_width = image.getWidth();
		_height = image.getHeight();
		_pix = new Pixel[_width][_height];
		
		int red;
		int green;
		int blue;
		
		for(int j=0; j<_height; j++)
		{
			for(int i=0; i<_width; i++)
			{
				red = image.getPixel(i, j).getRed();
				green = image.getPixel(i, j).getGreen();
				blue = image.getPixel(i, j).getBlue();
				_pix[i][j] = new Pixel((red*29 + green*59 + blue*11)/100);			
			}
		}
	}
	
	
	/* *********************************************************
	 *  Transform a BufferedImage in imageRGB 
	 * ********************************************************* */
	/**
	 * Create an ImagePix from an initial BufferedImage
	 * @param image original BufferedImage
	 */
	public ImagePix(BufferedImage image)
	{
		_width = image.getWidth();
		_height = image.getHeight();
		_pix = new Pixel[_width][_height];
		
		int imagePixel, imageRed, imageGreen, imageBlue; 

		for(int j=0; j<_height; j++)
		{
			for(int i=0; i<_width; i++)
			{
				imagePixel = image.getRGB(i, j);
				imageRed   = (imagePixel >> 16) & 0xFF;
				imageGreen = (imagePixel >> 8) & 0xFF;
				imageBlue  = (imagePixel  & 0xFF);
							
				_pix[i][j] = new Pixel(imageRed, imageGreen, imageBlue);			
			}
		}

	}
	
	
	/* *********************************************************
	 *  Transform a BufferedImage in imageGray
	 * ********************************************************* */
	/**
	 * Create an ImagePix from an initial BufferedImage and transform it in gray image
	 * @param image original BufferedImage
	 * @param gray indication of the transformation
	 */
	public ImagePix(BufferedImage image, String gray)
	{
		_width = image.getWidth();
		_height = image.getHeight();
		_pix = new Pixel[_width][_height];
		
		int imagePixel, imageRed, imageGreen, imageBlue; 

		for(int j=0; j<_height; j++)
		{
			for(int i=0; i<_width; i++)
			{
				imagePixel = image.getRGB(i, j);
				imageRed   = (imagePixel >> 16) & 0xFF;
				imageGreen = (imagePixel >> 8) & 0xFF;
				imageBlue  = (imagePixel  & 0xFF);
							
				_pix[i][j] = new Pixel((imageRed*29 + imageGreen*59 + imageBlue*11)/100);
			}
		}

	}
	
	
	/* *********************************************************
	 * Instance Methods
	 * *********************************************************
	 */
	/**
	 * Set the value of the specified Pixel
	 * @param x value on the x-axis of the Pixel
	 * @param y value on the y-axis of the Pixel
	 * @param pix value of the Pixel
	 */
	public void setPixel(int x, int y, Pixel pix)
	{
		if((x < _width) && (y < _height))
			_pix[x][y] = pix;
	}
	
	/**
	 * Get the Pixel at a specified position
	 * @param x value on the x-axis of the Pixel
	 * @param y value on the y-axis of the Pixel
	 * @return the Pixel at the specified position
	 */
	public Pixel getPixel(int x, int y)
	{
		if((x < _width) && (y < _height))
			return _pix[x][y];
		else
			return new Pixel();
	}
	
	
	/**
	 * Get the ImagePix width
	 * @return the width of the ImagePix
	 */
	public int getWidth()
	{
		return _width;
	}
	
	
	/**
	 * Get the ImagePix height
	 * @return the height of the ImagePix
	 */
	public int getHeight()
	{
		return _height;
	}
}
